<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button style="float:right" type="primary" @click="getDataList()" size="small">
          查询搜索
        </el-button>
        <el-button style="float:right;margin-right: 15px" @click="handleResetSearch()" size="small">
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" size="small" label-width="140px">
          <el-form-item label="输入表名称：">
            <el-input v-model="listQuery.tableName" class="input-width" placeholder="请输入表名称" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <div class="table-container">
      <el-table :data="list" border v-loading="dataListLoading" style="width: 100%;">
        <el-table-column prop="tableName" header-align="center" align="center" label="表名称">
        </el-table-column>
        <el-table-column prop="engine" header-align="center" align="center" label="数据库类型">
        </el-table-column>
        <el-table-column prop="tableComment" header-align="center" align="center" label="表备注">
        </el-table-column>
        <el-table-column prop="createTime" header-align="center" align="center" label="创建时间">
        </el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="genCode(scope.row.tableName)">代码生成</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper" :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize" :page-sizes="[10, 20, 50, 100]" :total="total">
      </el-pagination>
    </div>

  </div>
</template>

<script>
  import axios from 'axios';
  import { getToken } from '@/utils/auth'
  import { saveAs } from 'file-saver';
  import { fetchList } from '@/api/sys/gen';
  const baseURL = process.env.VUE_APP_BASE_API
  const defaultListQuery = {
    pageNum: 1,
    pageSize: 10,
    tableName: ''
  };
  export default {
    data() {
      return {
        listQuery: Object.assign({}, defaultListQuery),
        list: [],
        total: 0,
        dataListLoading: false,
      }
    },
    created() {
      this.getDataList()
    },
    methods: {
      // 重置查询表单
      handleResetSearch() {
        this.listQuery = Object.assign({}, defaultListQuery);
        this.getDataList();
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        fetchList(this.listQuery).then(response => {
          this.dataListLoading = false;
          this.list = response.data.list;
          this.total = response.data.total;
        })
      },
      // 每页数
      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.listQuery.pageNum = 1
        this.getDataList()
      },
      // 当前页
      handleCurrentChange(val) {
        this.listQuery.pageNum = val
        this.getDataList()
      },
      // 立即执行
      genCode(tableName) {
        this.$confirm(`确定生成代码?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          axios({
            method: 'get',
            url: baseURL + "/gen/code?tables=" + tableName,
            responseType: 'blob',
            headers: { 'Authorization': getToken() }
          }).then((res) => {
            if (res.data.type !== 'application/json') {
              const blob = new Blob([res.data], { type: 'application/zip' })
              saveAs(blob, tableName + ".zip")
            } else {
              this.printErrMsg(res.data);
            }
          })
        }).catch(() => {})
      },
    }
  }
</script>
